import {MenuApi} from "@/api/Menu/index.js";
import {defineStore} from "pinia";

export const MenuStores = defineStore('MenuStores', {
    state: () => ({
        MenuList: [],
        pageInfo: {
            page: 1,
            size: 10,
            total: 0,
            totalPage: 0
        }
    }),
    actions: {
        async list(params) {
            try {
                const response = await MenuApi.list({
                    page: params.pageNum || params.page || 1,
                    size: params.pageSize || params.size || 10,
                    ...params
                });

                if (response) {
                    this.MenuList = Array.isArray(response.data?.records) ? response.data.records : [];
                    this.pageInfo = {
                        page:  response.data.pageNumber || 1,
                        size:  response.data.pageSize || 10,
                        total: response.data.totalRow || 0,
                        totalPage:  response.data.totalPage || 0
                    }
                    return response;
                }
            } catch (error) {
                console.error('获取菜单列表失败:', error);
                throw error;
            }
        },

        async create(data) {
            try {
                const response = await MenuApi.create(data);
                return response;
            } catch (error) {
                console.error('创建菜单失败:', error);
                throw error;
            }
        },

        async delete(id) {
            try {
                const response = await MenuApi.delete(id);
                return response;
            } catch (error) {
                console.error('删除菜单失败:', error);
                throw error;
            }
        },

        async update(data) {
            try {
                const response = await MenuApi.update(data);
                return response;
            } catch (error) {
                console.error('更新菜单失败:', error);
                throw error;
            }
        }
    }
})